/*  SSEBDateFormatter.m
 *  MESLib
 * 
 * Copyright 2003-2009 Starline Software Ltd
 * 
 * This file is part of MESLib.
 * 
 * MESLib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MESLib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with MESLib. If not, see <http://www.gnu.org/licenses/>.
 */
//  Created by Tobias Haughton on Mon Feb 24 2003.
//  Copyright (c) 2003 Starline Software. All rights reserved.
//

#import "SSEBDateFormatter.h"


@implementation SSEBDateFormatter

- (BOOL)getObjectValue:(id *)anObject forString:(NSString *)string errorDescription:(NSString **)error
{
   BOOL retval = NO;

   if(![string isEqualToString:[NSString string]]) {
      retval = [super getObjectValue:anObject forString:string errorDescription:error];
   }

   //super turns the string in to an NSCalendarDate so we now need to make an SSChartDate out of that
   *anObject = [SSChartDate chartDateWithCalendarDate:(NSCalendarDate *)*anObject isEstimated:[string hasSuffix:@"e"]];

   return retval;
}

- (NSAttributedString *)attributedStringForObjectValue:(id)anObject withDefaultAttributes:(NSDictionary *)attributes
{

   return nil;   
}

- (NSString *)stringForObjectValue:(id)anObject
{
   if (![anObject isKindOfClass:[SSChartDate class]]) {
      return nil;
   }

   NSCalendarDate *cd = [[[NSCalendarDate alloc] initWithString:[NSString stringWithFormat:@"%@/%@/%@/%@/%@/%@/%@", [anObject descriptionWithCalendarFormat:@"%Y"],
      [anObject descriptionWithCalendarFormat:@"%m"], [anObject descriptionWithCalendarFormat:@"%d"], [anObject descriptionWithCalendarFormat:@"%H"],
      [anObject descriptionWithCalendarFormat:@"%M"], [anObject descriptionWithCalendarFormat:@"%S"], [anObject descriptionWithCalendarFormat:@"%F"]]
                                      calendarFormat:@"%Y/%m/%d/%H/%M/%S/%F"] autorelease];

   NSString *string = [super stringForObjectValue:cd];
   if([anObject isEstimated]) {
      //this date is tentative so add e
      string = [string stringByAppendingString:@" e"];
   }

   return string;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
   [super encodeWithCoder:coder];
}

- (id)initWithCoder:(NSCoder *)coder
{
   self = [super initWithCoder:coder];
   
   return self;
}

@end
